﻿using Blazui.Component;
using Microsoft.AspNetCore.Components;
using Project13776616780_01.Enums;
using Project13776616780_01.Model;
using Project13776616780_01.ViewModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;

namespace Project13776616780_01.Pages
{
    public partial class SystemSettings
    {
        [Inject]
        private DocsDbContext dbContext { get; set; }
        BForm form;
        private AppSettingModel model = new AppSettingModel();

        protected override async Task OnInitializedAsync()
        {
            await base.OnInitializedAsync();
            var settingRepo = dbContext.Set<AppSetting>();
            var properties = typeof(AppSettingModel).GetProperties();
            foreach (var property in properties)
            {
                var configName = Enum.Parse<AppSettingName>(property.Name);
                var setting = settingRepo.FirstOrDefault(x => x.Name == configName);
                if (setting == null)
                {
                    continue;
                }
                object value = setting.Value;
                if (value == null)
                {
                    continue;
                }
                if (property.Name == nameof(AppSettingModel.Logo))
                {
                    value = new UploadModel[]
                    {
                        new UploadModel()
                        {
                            Url=value.ToString(),
                            FileName=Path.GetFileName(value.ToString())
                        }
                    };
                }
                else
                {
                    value = Convert.ChangeType(value, property.PropertyType);
                }
                property.SetValue(model, value);
            }
            await InvokeAsync(Refresh);
        }

        private void SaveData()
        {
            if (!form.IsValid())
            {
                return;
            }
            var settingRepo = dbContext.Set<AppSetting>();
            var model = form.GetValue<AppSettingModel>();
            var properties = model.GetType().GetProperties();
            foreach (var property in properties)
            {
                var configName = Enum.Parse<AppSettingName>(property.Name);
                var setting = settingRepo.FirstOrDefault(x => x.Name == configName);
                if (setting != null)
                {
                    settingRepo.Remove(setting);
                }
                var value = property.GetValue(model);
                if (value is IFileModel[] files)
                {
                    if (property.Name == nameof(AppSettingModel.Logo))
                    {
                        value = files[0].Url;
                    }
                }
                settingRepo.Add(new AppSetting()
                {
                    Name = configName,
                    Value = value?.ToString()
                });
            }
            dbContext.SaveChanges();
            Toast("保存成功");
        }
    }
}
